package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionServices;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;
    @Reference
    private PermissionServices permissionService;

    //新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        try{
            roleService.add(map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);//新增失败
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);//新增成功
    }

    //编辑检查组
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map){
        try{
            roleService.edit(map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);//编辑失败
        }
        return new Result(true,MessageConstant.EDIT_RPLE_SUCCESS);//编辑成功
    }

    //删除角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            roleService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role>list = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);//查询失败
        }
    }

    //根据ID查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);//查询失败
        }
    }

    //根据角色ID查询角色包含的多个权限ID
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try{
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionIds);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);//查询失败
        }
    }
    //根据角色ID查询角色包含的多个菜单ID
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try{
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,menuIds);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);//查询失败
        }
    }
}
